package com.lanjian.idbank.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanjian.idbank.mapper.BankMapper;
import com.lanjian.idbank.pojo.BankEntity;
import com.lanjian.idbank.utils.RealBankCardAuthentication;
import com.lanjian.idbank.mapper.BankMapper;
import com.lanjian.modelo.result.Result;
import com.lanjian.modelo.result.ResultObject;
import com.lanjian.modelo.vo.BankVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description: new java files header..
 *
 * @author 兰剑
 * @version 1.0
 * @date 2023/3/27 15:31
 */
@Service
public class BankService {

    @Autowired
    private BankMapper bankMapper;



    /**@Description: 查询银行卡列表
     * @param bankVo
     * @return
     * @version v1.0
     * @author 兰剑
     * @date 2023/3/28 10:48
     */
    public Map<String, Object> findBank(BankVo bankVo) {
        QueryWrapper<BankEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("del",0);
//        判断银行卡不能为空(精确查询)
        if (bankVo.getBankNumber()!=null && bankVo.getBankNumber()!=""){
            queryWrapper.eq("bank_number",bankVo.getBankNumber());
        }
//        查询用户姓名（模糊查询）
        if (bankVo.getUname()!=null && bankVo.getUname()!=""){
            queryWrapper.like("uname",bankVo.getUname());
        }
//        查询用户手机号（精确查询）
        if (bankVo.getTelPhone()!=null && bankVo.getTelPhone()!=""){
            queryWrapper.eq("tel_phone",bankVo.getTelPhone());
        }
//        时间区间查询
        if (bankVo.getMinTime()!=null){
            queryWrapper.ge("create_time",bankVo.getMinTime());
        }
        if (bankVo.getMaxTime()!=null){
            queryWrapper.le("create_time",bankVo.getMaxTime());
        }
        //        开启分页
        Page<BankEntity> page=new Page<>(bankVo.getPageNum(),bankVo.getPageSize());
        Page<BankEntity> bankList = bankMapper.selectPage(page, queryWrapper);
//        获取集合数据
        List<BankEntity> records = bankList.getRecords();
        long total = bankList.getTotal();
//        创建hashmap集合返回到前端
        Map map=new HashMap();
        map.put("bankList",records);
        map.put("total",total);
        return map;
    }


    /**@Description: 添加银行卡
     * @param bankVo
     * @return
     * @version v1.0
     * @author 兰剑
     * @date 2023/3/28 10:48
     */
    public Result addBank(BankVo bankVo) {
//        获取前台值
        String bankNumber = bankVo.getBankNumber();
//        获取前台所属银行
        String bankForm = bankVo.getBankForm();
        String uname = bankVo.getUname();
        String phone = bankVo.getTelPhone();
        String idCard = bankVo.getIdCard();
        Integer status = bankVo.getStatus();
        QueryWrapper<BankEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(BankEntity::getStatus,status)
                .or()
                .eq(BankEntity::getBankNumber,bankNumber);
//        查询数据
        List<BankEntity> bankEntities = bankMapper.selectList(queryWrapper);
        BankEntity bankStatus = bankEntities.get(0);
        if (bankStatus.getStatus()==1){
            return new Result(true,"1","用户已绑定，请先解除绑定");
        }
        Integer integers = bankMapper.selectCount(queryWrapper);
//        调用接口是否重复
        if (integers>1){
            return new Result(true,"1","银行卡已被添加，不能重复添加");
        }
//        调用接口验证银行卡真实性
        Integer integer = RealBankCardAuthentication.VerifyBankCard(uname,phone, idCard, bankNumber);
//        逻辑处理判断 1代表通过，2代表不通过
        if (integer==1){
            return new Result(true,"1","银行卡可以用，请绑定用户");
        }
        BankEntity bank=new BankEntity();
        bank.setBankNumber(bankNumber);
        bank.setBankForm(bankForm);
        bank.setCreateTime(new Date());
        bank.setUpdateTime(new Date());
//        调用接口完成添加
        bankMapper.insert(bank);
        return new Result(true,"1","添加成功");
    }


    /**@Description: 删除银行卡
     * @param bid
     * @return
     * @version v1.0
     * @author 兰剑
     * @date 2023/3/28 10:49
     */
    public Result delBank(Integer bid) {
//        调用接口完成删除
        bankMapper.deleteById(bid);
        return new Result(true,"1","删除成功");
    }

    /**@Description: 修改银行卡
     * @param  bankVo
     * @return
     * @version v1.0
     * @author 兰剑
     * @date 2023/3/28 10:49
     */
    public Result updateBank(BankVo bankVo) {
        QueryWrapper<BankEntity> queryWrapper=new QueryWrapper<>();
        List<BankEntity> bankEntities = bankMapper.selectList(queryWrapper);
        BankEntity bank = bankEntities.get(0);
        bank.setBid(bankVo.getBid());
        bank.setBankNumber(bankVo.getBankNumber());
        bank.setBankForm(bankVo.getBankForm());
        bank.setUname(bankVo.getUname());
        bank.setIdCard(bankVo.getIdCard());
        bank.setTelPhone(bankVo.getTelPhone());
        bank.setUpdateTime(new Date());
//        执行修改
        bankMapper.updateById(bank);
        return new Result(true,"1","修改成功");
    }

    /**@Description: 移除展示银行卡信息（逻辑删除）
     * @param bankVo
     * @return
     * @version v1.0
     * @author 兰剑
     * @date 2023/3/28 10:49
     */
    public Result isDelBank(BankVo bankVo) {
        QueryWrapper<BankEntity> queryWrapper=new QueryWrapper<>();
        List<BankEntity> bankEntities = bankMapper.selectList(queryWrapper);
        BankEntity bank = bankEntities.get(0);
//            执行逻辑删除
            bank.setDel(1);
            bankMapper.updateById(bank);
            return new Result(true,"1","删除成功");

    }


    /**@Description: 绑定银行卡
     * @param bankVo
     * @return
     * @version v1.0
     * @author 兰剑
     * @date 2023/3/28 10:50
     */
    public ResultObject<BankEntity> bindingBank(BankVo bankVo) {
        Integer status = bankVo.getStatus();
        QueryWrapper<BankEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(BankEntity::getStatus,status);
        List<BankEntity> bankEntities = bankMapper.selectList(queryWrapper);
        BankEntity bank = bankEntities.get(0);
        bank.setDel(1);
        return new ResultObject<>(true,"1","绑定成功",null);

    }


    /**@Description: 解除绑定
     * @param bankVo
     * @return
     * @version v1.0
     * @author 兰剑
     * @date 2023/3/28 10:50
     */
    public Result removeBank(BankVo bankVo) {
        Integer status = bankVo.getStatus();
        QueryWrapper<BankEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(BankEntity::getStatus,status);
        List<BankEntity> bankEntities = bankMapper.selectList(queryWrapper);
        BankEntity bank = bankEntities.get(0);
        bank.setStatus(2);
        return new Result(true,"1","解绑成功");
    }
}
